<?php
header('Content-Type: text/plain; charset=utf-8');
define( '_BOOTSTRAP', 1 );
include('../stderr.php');
include('../library/globals.php');
include('../library/database.php');

function json_format($json)
{
    $tab = "  ";
    $new_json = "";
    $indent_level = 0;
    $in_string = false;

    $json_obj = json_decode($json);

    if(!$json_obj)
        return false;

    $json = json_encode($json_obj);
    $len = strlen($json);

    for($c = 0; $c < $len; $c++)
    {
        $char = $json[$c];
        switch($char)
        {
            case '{':
            case '[':
                if(!$in_string)
                {
                    $new_json .= $char . "\n" . str_repeat($tab, $indent_level+1);
                    $indent_level++;
                }
                else
                {
                    $new_json .= $char;
                }
                break;
            case '}':
            case ']':
                if(!$in_string)
                {
                    $indent_level--;
                    $new_json .= "\n" . str_repeat($tab, $indent_level) . $char;
                }
                else
                {
                    $new_json .= $char;
                }
                break;
            case ',':
                if(!$in_string)
                {
                    $new_json .= ",\n" . str_repeat($tab, $indent_level);
                }
                else
                {
                    $new_json .= $char;
                }
                break;
            case ':':
                if(!$in_string)
                {
                    $new_json .= ": ";
                }
                else
                {
                    $new_json .= $char;
                }
                break;
            case '"':
                $in_string = !$in_string;
            default:
                $new_json .= $char;
                break;
        }
    }

    return $new_json;
}

$database = new database('localhost', 'pstrychacz', 'szpaner1', 'poi', '3306');
$database->setCharacter('utf8');
$idListy = $_GET['id'];

$select = "SELECT points.id , points.name, points.desc AS descr, lat, lon
FROM (points LEFT JOIN cat ON points.id_cat = cat.id )
LEFT JOIN `list-poi` on points.id = `list-poi`.ID_POINT
WHERE `list-poi`.ID_LIST = ".$idListy;
$database->query($select);

$allMarkers = array();
$markers = array();

while($dane = mysql_fetch_array($database->result))
{
   $markers['id'] = $dane['id'];
   $markers['lat'] = floatval($dane['lat']);
   $markers['lon'] = floatval($dane['lon']);
   $markers['name'] = htmlspecialchars($dane['name']);
   $markers['desc'] = htmlspecialchars($dane['descr']);
   $allMarkers[] = $markers;
}
$out = json_encode($allMarkers);
//echo $out;
echo json_format($out);
?>
